package LCR;

public class isSubtree {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null) return false;
        if(isSametree(root,subRoot)) {
            return true;
        }
        if(isSubtree(root.left,subRoot)){
            return true;
        }
        if(isSubtree(root.right,subRoot)){
            return true;
        }
        return false;
    }
    boolean isSametree(TreeNode root,TreeNode subRoot) {
        if(root == null && subRoot != null || root != null && subRoot == null) {
            return false;
        }
        if(root == null && subRoot == null) {
            return true;
        }
        if(root.val !=subRoot.val) {
            return false;
        }
        return isSametree(root.left,subRoot.left) && isSametree(root.right,subRoot.right);
    }
}
